package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    private void deleteDish(){

        redisTemplate.delete(RedisConstant.CATEGORY_CACHE_PREFIX + "*");
    }


    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);


        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dish.getId()));
        }
        dishFlavorMapper.saveFlavor(flavors);
        deleteDish();
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        dishFlavorMapper.deletesFlavorById(List.of(dish.getId()));

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor ->
                    dishFlavor.setDishId(dish.getId()));
        }
        dishFlavorMapper.saveFlavor(flavors);
        deleteDish();
    }

    @Override
    public List<Dish> findBycategoryId(Long categoryId) {
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        return dishMapper.findBycategoryId(dish);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deletes(List<Long> ids) {
        Integer count1 = dishMapper.queryCountStatus(ids, StatusConstant.ENABLE);
        if (count1>0){
            throw new BaseException("菜品正在售卖中");
        }
        Integer count2 =  dishMapper.queryCountByDishIds(ids);
        if (count2 > 0){
            throw new BaseException("菜品包含在套餐中");
        }
        dishMapper.deletes(ids);
        dishFlavorMapper.deletesFlavorById(ids);
        deleteDish();
    }

    @Override
    public DishVO findById(Integer id) {
        DishVO dishVO = dishMapper.findById(id);
        dishVO.setFlavors(dishFlavorMapper.findFlavorById(id));
        return dishVO;
    }


}
